Skip to content

Move ethkey crypto utils to parity crypto crate#210

Merged
grbIzl merged 21 commits into
masterfrom
EthKeyCrypto
Oct 22, 2019
Merged

Move ethkey crypto utils to parity crypto crate#210
grbIzl merged 21 commits into
masterfrom
EthKeyCrypto

Conversation

@grbIzl
Copy link
Copy Markdown
Contributor

@grbIzl grbIzl commented Sep 2, 2019

The intention of this PR is to remove dependency to accounts\ethkey for parity components, that use crypto utils from it. That would allow further decomposition of parity repo. Including move of secret store into the separate repo.
Additional comments:

  1. I straightforwardly merged code from ethkey into parity crypto, that is responsible for work with key pairs, encryption etc.
  • I left wallets code (brain, brain prefix etc) in ethkey. Also left password.rs there
  • I didn't do any renaming and reorganization of parity crypto crate. May be it's worth doing, but I don't have good ideas and would be grateful for any suggestions.
  1. I'm not going to consider substrate code, related to the same crypto primitives. It's left for the further development.

@parity-cla-bot

This comment has been minimized.

@grbIzl grbIzl added the M4-core label Sep 2, 2019
Copy link
Copy Markdown
Contributor

@ordian ordian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked deeply at the code as it seems to be copied, but have some concerns about dependencies.

Comment thread parity-crypto/Cargo.toml Outdated
Comment thread parity-crypto/Cargo.toml Outdated
Comment thread parity-crypto/Cargo.toml Outdated
Comment thread parity-crypto/Cargo.toml
Copy link
Copy Markdown
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I'm not clear about is if the code added here is entierly ethereum-specific or if there is stuff that is useful to other projects as well. You mentioned secret-store might be extracted after this. Are there other Parity projects that use/could use the crypto here?
/cc @cheme

Comment thread parity-crypto/Cargo.toml Outdated
Comment thread parity-crypto/src/crypto.rs Outdated
Comment thread parity-crypto/Cargo.toml Outdated
Comment thread parity-crypto/src/crypto.rs Outdated
Comment thread parity-crypto/src/crypto.rs Outdated
Comment thread parity-crypto/src/secret.rs
Comment thread parity-crypto/src/secret.rs
}

/// Inplace add one secret key to another (scalar + scalar)
pub fn add(&mut self, other: &Secret) -> Result<(), Error> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a lot of similar mathy methods here as well as in math.rs: maybe there's a trait here waiting to be discovered and impl'd for Secret and Public? add, sub, mul, neg, is_valid etc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's different algebraic groups (scalar and elliptic curve). I'm afraid, that general trait for them could lead to mistreatments

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mistreatments, can you elaborate on that? How could it be abused?

Comment thread parity-crypto/src/signature.rs
Comment thread parity-crypto/src/signature.rs Outdated
@ordian
Copy link
Copy Markdown
Contributor

ordian commented Sep 3, 2019

Also see #80 for previous attempt to add secp256k1 to parity-crypto (cc @cheme).

@grbIzl
Copy link
Copy Markdown
Contributor Author

grbIzl commented Sep 3, 2019

Also see #80 for previous attempt to add secp256k1 to parity-crypto (cc @cheme).

IMHO @cheme was too ambitious in his PR :-) I want to achieve simpler objectives (move common code from ethkey and don't break anything dependent on parity-crypto). And we need to have it done, if we want to develop secret store further as a separate project. So I hope to bring it to the (happy) end.

@grbIzl
Copy link
Copy Markdown
Contributor Author

grbIzl commented Sep 3, 2019

One thing I'm not clear about is if the code added here is entierly ethereum-specific or if there is stuff that is useful to other projects as well. You mentioned secret-store might be extracted after this. Are there other Parity projects that use/could use the crypto here?

The primary beneficiary of this PR is secret store indeed. I'm not aware of other projects for sure. At the same time, we wanted to extract account management, removing this dependency from it could help.

@grbIzl
Copy link
Copy Markdown
Contributor Author

grbIzl commented Sep 3, 2019

@dvdplm thanks for comments! It will be good to cleanup this code, as it was not touched for a while.

@dvdplm
Copy link
Copy Markdown
Contributor

dvdplm commented Sep 3, 2019

@grbIzl I really appreciate the effort and the goal here! FWIW I tried something similar a while back and failed.
It's a substantial chunk of code added though so I want to collect opinions from more people on where we want this to live. I think it's good to get as many eyes on it as possible so we avoid bitrot.

@dvdplm
Copy link
Copy Markdown
Contributor

dvdplm commented Sep 3, 2019

Are there other Parity projects that use/could use the crypto here?

Substrate uses parity-crypto in substrate-keystore, @grbIzl, we need to make sure that when this PR is in good shape we submit upgrade PRs to both parity-ethereum and substrate.

@grbIzl
Copy link
Copy Markdown
Contributor Author

grbIzl commented Sep 16, 2019

I put newly added dependencies under the build feature (publickey).

The only not implemented major thing is including of ethereum_types crate. H256 is used heavily in Signature and Secret classes and I would need to copypaste its functionality there. So for now I leave it as a dependency (but put under the same feature)

Copy link
Copy Markdown
Contributor

@ordian ordian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general, some minor suggestions.

Comment thread parity-crypto/Cargo.toml Outdated
Comment thread parity-crypto/Cargo.toml Outdated
Comment thread parity-crypto/src/publickey/error.rs Outdated
Copy link
Copy Markdown
Collaborator

@cheme cheme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With things moved inside a feature gated module, I guess we can have ethereum-type dependency and some former code moved with less hesitation.

Comment thread parity-crypto/src/publickey/mod.rs
Comment thread parity-crypto/Cargo.toml

[features]
default = []
publickey = ["eth-secp256k1", "lazy_static", "ethereum-types"] No newline at end of file
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about the rational of naming the feature 'publickey' , a comment could be good.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to name this feature secp256k1? publickey seems too general

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree in general, but this exact name (secp256k1) seems not the best option, because we already have eth-secp256k1 feature and it will be two similar names. Naming is the hardest part of coding :-(

Copy link
Copy Markdown
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff here. I got ~half way through. Mostly nits and (I believe) mostly on old code that was copied over.

Comment thread parity-crypto/Cargo.toml Outdated
Comment thread parity-crypto/Cargo.toml
Comment thread parity-crypto/Cargo.toml
Comment thread parity-crypto/src/publickey/ec_math_utils.rs Outdated
Comment thread parity-crypto/src/publickey/ec_math_utils.rs Outdated
Comment thread parity-crypto/src/publickey/ecdsa_signature.rs
Comment thread parity-crypto/src/publickey/ecdsa_signature.rs
Comment thread parity-crypto/src/publickey/ecdsa_signature.rs
Comment thread parity-crypto/src/publickey/ecdsa_signature.rs
Comment thread parity-crypto/src/publickey/ecdsa_signature.rs
@dvdplm
Copy link
Copy Markdown
Contributor

dvdplm commented Sep 16, 2019

I think it's unfortunate that git history has not been preserved for these files as they migrated from the parity-ethereum repo. If it's too complex to do then we should at least make a note of where to look for it back in ethkey.

@grbIzl
Copy link
Copy Markdown
Contributor Author

grbIzl commented Sep 18, 2019

I think it's unfortunate that git history has not been preserved for these files as they migrated from the parity-ethereum repo. If it's too complex to do then we should at least make a note of where to look for it back in ethkey.

It's a common problem for such case, when files are moved to the other repo. So I've put a comment, but not sure, how helpful it will be

Copy link
Copy Markdown
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a few minor nits

Comment thread parity-crypto/src/publickey/ec_math_utils.rs
Comment thread parity-crypto/src/publickey/ec_math_utils.rs
let secret = Secret::from_str("a100df7a048e50ed308ea696dc600215098141cb391e9527329df289f9383f65").unwrap();
let mut public = generation_point();
public_mul_secret(&mut public, &secret).unwrap();
assert_eq!(format!("{:x}", public), "8ce0db0b0359ffc5866ba61903cc2518c3675ef2cf380a7e54bde7ea20e6fa1ab45b7617346cd11b7610001ee6ae5b0155c41cad9527cbcdff44ec67848943a4");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this value is from a spec somewhere it'd be great to document that. :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a randomly created secret

Comment thread parity-crypto/Cargo.toml
name = "bench"
harness = false

required-features = ["publickey"]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@dvdplm
Copy link
Copy Markdown
Contributor

dvdplm commented Sep 23, 2019

It's a common problem for such case, when files are moved to the other repo.

It is. The trick is to using something like git subtree extract. I should have spoken up when you started this work. :/

Copy link
Copy Markdown
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few minor things

Comment thread parity-crypto/Cargo.toml
[package]
name = "parity-crypto"
version = "0.4.1"
version = "0.4.2"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought: while it's technically correct that the changes here are not breaking the previous API, I wonder if it's not warranted to go with 0.5 here anyway, just to signal the presence of a major new piece of functionality?
/cc @ordian

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see where you're coming from and don't have a strong opinion on this. Having a minor release makes it easier to ensure other crates could use the latest version of parity-crypto (even via transitive dependencies), but that's a minor concern.

}

/// Compute power of secret key inplace (secret ^ pow).
/// This function is not intended to be used with large powers.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? And what is the limit? And what happens if one does? Panic? Panic or overflowing behaviour should be documented.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this comment as it's misleading. The only limitation here is pow (usize). The multiplication on elliptic curves doesn't have any limitations

Comment thread parity-crypto/src/publickey/secret_key.rs Outdated
Comment thread parity-crypto/src/publickey/secret_key.rs Outdated
Comment thread parity-crypto/src/publickey/secret_key.rs Outdated
Comment thread parity-crypto/src/publickey/keypair.rs
Comment thread parity-crypto/src/publickey/keypair.rs
Comment thread parity-crypto/src/publickey/extended_keys.rs Outdated
Comment thread parity-crypto/src/publickey/error.rs
Comment thread parity-crypto/src/publickey/ecies.rs Outdated
Copy link
Copy Markdown
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

So what are the next steps after this? A PR to parity-ethereum to use this I guess?

@grbIzl
Copy link
Copy Markdown
Contributor Author

grbIzl commented Sep 24, 2019

So what are the next steps after this? A PR to parity-ethereum to use this I guess?

Yep.

@dvdplm
Copy link
Copy Markdown
Contributor

dvdplm commented Sep 24, 2019

k, I'm putting this onice so it doesn't get merged before it's time: @grbIzl will prepare a PR to parity-ethereum to make double extra sure nothing breaks and then we can merge this.

Comment thread parity-crypto/Cargo.toml

[dependencies]
tiny-keccak = "1.4"
eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1", rev = "a96ad75", optional = true }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1", rev = "a96ad75", optional = true }
eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1", rev = "72c93ab = true }

another update

@grbIzl grbIzl merged commit 0de0098 into master Oct 22, 2019
@grbIzl grbIzl deleted the EthKeyCrypto branch October 22, 2019 07:51
@niklasad1
Copy link
Copy Markdown
Contributor

@grbIzl @dvdplm let's publish a new version of parity-crypto so it can be used by openethereum/parity-ethereum#11174?

@grbIzl
Copy link
Copy Markdown
Contributor Author

grbIzl commented Oct 22, 2019

@grbIzl @dvdplm let's publish a new version of parity-crypto so it can be used by paritytech/parity-ethereum#11174?

Working on it. Right now bumped into paritytech/rust-secp256k1#25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants